import { Body, Controller, Get, Param, Post, Query, UseGuards } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { PostService } from './post.service';
import { CurrentUser } from 'src/user/user.decorator';
import { UserSys } from 'src/user/userSys.entity';
import { UseLog } from 'src/log/useLog.decorator';
import { AuthGuard } from '@nestjs/passport';
import * as moment from 'moment';
import { Between, FindOptionsWhere } from 'typeorm';
import { UserService } from 'src/user/user.service';
import { RoleEnum } from 'src/role/role.enum';

@Controller('api/post')
@ApiTags("报表")
@UseGuards(AuthGuard("jwt"))
export class PostController {
    constructor(
        private readonly postService: PostService,
        private readonly userService: UserService
    ) { }

    @Get("get")
    @ApiOperation({ summary: "获取某月报表" })
    get(
        @Query("date") date: string,
        @CurrentUser() user: UserSys
    ) {
        if (!date) date = moment(Date.now()).format("YYYY-MM")
        const time = moment(date);
        return this.postService.post.find({
            where: {
                user: {
                    id: user.id
                },
                createAt: Between(
                    time.clone().startOf("month").toDate(),
                    time.clone().endOf("month").toDate()
                )
            }
        })
    }

    @Get("getMine")
    @ApiOperation({ summary: "获取我的报表" })
    async getMine(
        @CurrentUser() user: UserSys,
        @Query("page") page: number,
        @Query("limit") limit: number
    ) {
        if (!limit) limit = 10;
        if (!page) page = 1;
        const [data, count] = await this.postService.post.findAndCount({
            order: {
                id: "desc"
            },
            where: {
                user: {
                    id: user.id
                }
            },
            skip: (page - 1) * limit,
            take: limit,
        })
        return { data, count }
    }

    @Get("getAll")
    @ApiOperation({ summary: "获取所有报表" })
    async getAll(
        @Query("date") date: string,
        @CurrentUser() user: UserSys
    ) {
        if (!date) date = moment(Date.now()).format("YYYY-MM")
        const time = moment(date);
        const userWhere: FindOptionsWhere<UserSys> = {
            system: {
                id: user.system.id
            }
        }
        if (user.role === RoleEnum.manager) {
            userWhere.dept = {
                id: user.dept.id
            }
        }
        return {
            users: await this.userService.userSys.find({
                where: userWhere,
                relations: ["dept"]
            }),
            posts: await this.postService.post.find({
                where: {
                    user: userWhere,
                    createAt: Between(
                        time.clone().startOf("month").toDate(),
                        time.clone().endOf("month").toDate()
                    )
                },
                relations: ["user"]
            })
        }
    }

    @Get("getCurrent")
    @ApiOperation({ summary: "获取今日报表" })
    getCurrent(
        @CurrentUser() user: UserSys
    ) {
        return this.postService.getCurrent(user)
    }

    @Post("publish")
    @UseLog("提交报表")
    async publish(
        @CurrentUser() user: UserSys,
        @Body() { ...rest }
    ) {
        const entity = await this.postService.getCurrent(user);
        this.postService.post.save({ ...entity, ...rest })
    }

    @Get("user/:id/get")
    @ApiOperation({ summary: "获取员工报表" })
    async userGet(
        @Query("limit") limit: number,
        @Query("page") page: number,
        @Param("id") id: number
    ) {
        if (!limit) limit = 10;
        if (!page) page = 1;
        const [data, count] = await this.postService.post.findAndCount({
            where: {
                user: {
                    id
                }
            },
            take: limit,
            skip: (page - 1) * limit,
            order: { id: "desc" }
        })
        return { data, count }
    }
}
